/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.viewer.mapimport;


import com.lucidhex.LucidWorld;
import com.lucidhex.hex.LucidHexType;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Wolf
 */
public class ColorListModel implements ComboBoxModel{
    ArrayList<ListDataListener> listeners = new ArrayList<>();
    HashMap<Color, LucidHexType> data = new HashMap<>();
    ArrayList<Color> list = new ArrayList<>();
    private LucidWorld world;
    public ColorListModel(){
    }
    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public Object getElementAt(int i) {
        Color aColor = list.get(i);
        String hexType = null;
        if(data.get(aColor)!= null){
            hexType = data.get(aColor).getTypeName();
        }
        return new ColorListIcon(hexType,aColor);
    }

    public LucidHexType getValue(Color color) {
        return data.get(color);
    }

    public Set<Color> keySet() {
        return data.keySet();
    }

    public void put(Color color, LucidHexType aHex) {
        LucidHexType oldHex = data.put(color, aHex);
        if(oldHex != null){
            int i = list.indexOf(color);
            list.remove(color);
            fireIntervalRemoved(this, i,i);
        }
        list.add(color);
        fireIntervalAdded(this, list.size()-1, list.size()-1);
    }

    public final void reset() {
        int size = data.size();
        data.clear();
        list.clear();
        if(size > 0){
            fireIntervalRemoved(this, 0, size-1);
        }
        if(world !=null){
            for(LucidHexType lht : world.getBoard().getAvailbleHexTypes()){
                put(lht.getBackgroundColor(), lht);
            }
        }
        put(Color.WHITE, null);
    }

    @Override
    public void setSelectedItem(Object o) {
        // not allowed to select
    }

    @Override
    public Object getSelectedItem() {
        return null;
    }

    @Override
    public void addListDataListener(ListDataListener ll) {
        listeners.add(ll);
    }

    @Override
    public void removeListDataListener(ListDataListener ll) {
        listeners.remove(ll);
    }

    private void fireIntervalRemoved(ColorListModel aThis, int i, int i0) {
        for(ListDataListener ll : listeners){
            ll.intervalRemoved(new ListDataEvent(aThis,ListDataEvent.INTERVAL_REMOVED, i, i0));
        }
    }

    private void fireIntervalAdded(ColorListModel aThis, int i, int i0) {
        for(ListDataListener ll : listeners){
            ll.intervalAdded(new ListDataEvent(aThis,ListDataEvent.INTERVAL_ADDED, i, i0));
        }
    }

    void setBaseWorld(LucidWorld world) {
        this.world = world;
        reset();
    }

    
}
